package com.chen.dataGet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.LoggerFactory;

import com.chen.entity.Book;

import ch.qos.logback.classic.Logger;

	/**
	* @author chen
	* @date ${2016-10-30}
	* @version 1.0
	*/

public class DataGet {
	  private static Logger log = (Logger) LoggerFactory.getLogger(DataGet.class);
	  
	  static Map<String,Book> map = new HashMap<String,Book>();//存储所有图书数据
	  static Map<String,Book> mapSort = new HashMap<String,Book>();//存储评价人数大于1000的数据
	
	  /*
	   * 获取互联网方面的书	  
	   */
	  
	  @SuppressWarnings("unused")
	public static void dataGetInternet(){
		  String urlPre = "https://book.douban.com/tag/互联网?start=";
		  
		  String urlSuf= "&type=T";
		  
		  Document doc = null;
		  String wrong = "";  
		  int count = 0;
  
		  do{
			  String numberStr = String.valueOf(count);
			  String url = urlPre+numberStr+urlSuf;//获取每一页互联网数据信息
			  count = count + 20;
				 
			  try {
				doc = Jsoup.connect(url).maxBodySize(10 * 1024 * 1024).timeout(20000).get();
				wrong = doc.select(".pl2").get(0).text();
			  }catch (final Exception e) {
			      DataGet.log.error("[DATAGET-ERROR] {}", e);
			  	}
			  
			 final Elements uls = doc.select("ul");
			 
			 final Element ul = uls.get(2);
			 
			 final Elements lis = ul.select("li");
		 
			 for(final Element li : lis){
				 Book book = new Book();
					
				 String bookName = li.select(".info").select("a").get(0).text();//获取书名
				 String href = li.select(".info").select("a").attr("href");//用来判断某本书是否同时属于互联网,编程,算法中的两种或三种
				 book.setBookName(bookName);
				 String inf = li.select(".pub").text();
				 String str[] = inf.split("/");
				 if(str.length>4){
					 String author = str[0];//获取作者名
					 book.setAuthor(author);
					 String press = str[str.length-3];//获取出版社
					 book.setPress(press);
					 String date = str[str.length-2];//获取出版日期
					 book.setDate(date);
					 String value = StringUtils.replace(str[str.length-1],"元","");//获取价钱
					 book.setValue(value);
					 String score = li.select(".info").select(".clearfix").select(".rating_nums").text();//获取评分
					 book.setScore(score);
					 String number = StringUtils.substring(li.select(".info").select(".clearfix").select(".pl").text(),1, -4);//获取评价人数
					 int num = 0;
					 double score2 = 0.0;
					 if(!number.equals("少于10")){
						 num = Integer.valueOf(number);
					 }
					 book.setNumber(number);
					 if(num>=1000){
						 score2 = Double.valueOf(score);
						 mapSort.put(href, book);
					 }
					 final Object[] params = new Object[]{bookName,score,num,author,press,date,value};
					 DataGet.log.debug("[NAME]{}",ArrayUtils.toString(params));
					 map.put(href,book);
				 }
				 else if(str.length==3){
					 String author = str[0];//获取作者名
					 book.setAuthor(author);
					 String press = str[1];//获取出版社
					 String date = "";//获取出版日期
					 String value = "";//获取价钱
					 for(int i=0;i<str.length;i++){
						 if(str[i].trim().startsWith("20")||str[i].trim().startsWith("19")){
							 date = str[i]; 
							 book.setDate(date);
						 if(i==1){
							 value = StringUtils.replace(str[i+1],"元","");
							 book.setDate(date);
						 }if(i==1){
							 press = str[i-1];
							 book.setPress(press);
						 }
						 }
					 }
					 String score = li.select(".info").select(".clearfix").select(".rating_nums").text();//获取评分
					 book.setScore(score);
					 String number = StringUtils.substring(li.select(".info").select(".clearfix").select(".pl").text(),1, -4);//获取评价人数
					 int num = 0;
					 if(!number.equals("少于10")){
						 num = Integer.valueOf(number);
					 }
					 book.setNumber(number);
					 double score2 = 0.0;
					 if(num>=1000){
						 score2 = Double.valueOf(score);
						 score2 = Double.valueOf(score);
						 mapSort.put(href, book);
					 }
					 final String[] params = new String[]{bookName,author,date,value};
					 DataGet.log.debug("[NAME]{}",ArrayUtils.toString(params));
					 map.put(href,book);
				 }		
				 else if(str.length==4){
					 String author = str[0];//获取作者名
					 book.setAuthor(author);
					 String press = str[1];//获取出版社
					 String date = "";//获取出版日期
					 String value = str[3];//获取价钱
					 for(int i=0;i<str.length;i++){
						 if((str[i].trim().startsWith("20")) || (str[i].trim().startsWith("19"))){
							 date = str[i];
							 book.setDate(date);
						 if(i==2){
							 value = StringUtils.replace(str[i+1],"元","");
							 book.setValue(value);
							 press = str[i-1];
							 book.setPress(press);
						 }
						 }
					 }
					 String score = li.select(".info").select(".clearfix").select(".rating_nums").text();//获取评分
					 book.setScore(score);
					 String number = StringUtils.substring(li.select(".info").select(".clearfix").select(".pl").text(),1, -4);//获取评价人数
					 int num = 0;
					 if(!number.equals("少于10")){
						 num = Integer.valueOf(number);
					 }
					 book.setNumber(number);
					 double score2 = 0.0;
					 if(num>=1000){
						 score2 = Double.valueOf(score);
						 score2 = Double.valueOf(score);
						 mapSort.put(href, book);
					 }
					 final String[] params = new String[]{bookName,author,date,value};
					 DataGet.log.debug("[NAME]{}",ArrayUtils.toString(params));
					 map.put(href,book);
				 }
				 else if(str.length==2){
					 String author = str[0];//获取作者名
					 book.setAuthor(author);
					 String date = "";
					 String value = "";
					 if(str[1].trim().startsWith("20")||str[1].trim().startsWith("19")){
						 date = str[1];
						 book.setDate(date);
					 }else{
						 value = str[1];
						 book.setValue(value);
					 }
					 String score = li.select(".info").select(".clearfix").select(".rating_nums").text();//获取评分
					 book.setScore(score);
					 String number = StringUtils.substring(li.select(".info").select(".clearfix").select(".pl").text(),1, -4);//获取评价人数
					 int num = 0;
					 if(!number.equals("少于10")){
						 num = Integer.valueOf(number);
					 }
					 book.setNumber(number);
					 double score2 = 0.0;
					 if(num>=1000){
						 score2 = Double.valueOf(score);
						 mapSort.put(href, book);
					 }
					 final String[] params = new String[]{bookName,author,date};
					 DataGet.log.debug("[NAME]{}",ArrayUtils.toString(params));	
					 map.put(href,book);
				 }
			 }
		  }while(!wrong.equals("没有找到符合条件的图书"));	  		
	 }
	  
		/*
		 * 获取算法方面的书	  
		 */

	@SuppressWarnings("unused")
	public static void dataGetAlgorithm(){
	  String urlPre = "https://book.douban.com/tag/算法?start=";
	  
	  String urlSuf= "&type=T";
	  
	  Document doc = null;
	  String wrong = "";  
	  int count = 0;

	  do{
		  String numberStr = String.valueOf(count);
		  String url = urlPre+numberStr+urlSuf;//获取每一页算法数据信息
		  count = count + 20;
			 
		  try {
			doc = Jsoup.connect(url).maxBodySize(10 * 1024 * 1024).timeout(20000).get();
			wrong = doc.select(".pl2").get(0).text();
		  }catch (final Exception e) {
		      DataGet.log.error("[DATAGET-ERROR] {}", e);
		  	}
		  
		 final Elements uls = doc.select("ul");
		 
		 final Element ul = uls.get(2);
		 
		 final Elements lis = ul.select("li");
	 
		 for(final Element li : lis){
			 Book book = new Book();
				
			 String bookName = li.select(".info").select("a").get(0).text();//获取书名
			 String href = li.select(".info").select("a").attr("href");//用来判断某本书是否同时属于互联网,编程,算法中的两种或三种
			 book.setBookName(bookName);
			 String inf = li.select(".pub").text();
			 String str[] = inf.split("/");
			 if(str.length>4){
				 String author = str[0];//获取作者名
				 book.setAuthor(author);
				 String press = str[str.length-3];//获取出版社
				 book.setPress(press);
				 String date = str[str.length-2];//获取出版日期
				 book.setDate(date);
				 String value = StringUtils.replace(str[str.length-1],"元","");//获取价钱
				 book.setValue(value);
				 String score = li.select(".info").select(".clearfix").select(".rating_nums").text();//获取评分
				 book.setScore(score);
				 String number = StringUtils.substring(li.select(".info").select(".clearfix").select(".pl").text(),1, -4);//获取评价人数
				 int num = 0;
				 double score2 = 0.0;
				 if(!number.equals("少于10")){
					 num = Integer.valueOf(number);
				 }
				 book.setNumber(number);
				 if(num>=1000){
					 score2 = Double.valueOf(score);
					 mapSort.put(href, book);
				 }
				 final Object[] params = new Object[]{bookName,score,num,author,press,date,value};
				 DataGet.log.debug("[NAME]{}",ArrayUtils.toString(params));
				 map.put(href,book);
			 }
			 else if(str.length==3){
				 String author = str[0];//获取作者名
				 book.setAuthor(author);
				 String press = str[1];//获取出版社
				 String date = "";//获取出版日期
				 String value = "";//获取价钱
				 for(int i=0;i<str.length;i++){
					 if(str[i].trim().startsWith("20")||str[i].trim().startsWith("19")){
						 date = str[i]; 
						 book.setDate(date);
					 if(i==1){
						 value = StringUtils.replace(str[i+1],"元","");
						 book.setDate(date);
					 }if(i==1){
						 press = str[i-1];
						 book.setPress(press);
					 }
					 }
				 }
				 String score = li.select(".info").select(".clearfix").select(".rating_nums").text();//获取评分
				 book.setScore(score);
				 String number = StringUtils.substring(li.select(".info").select(".clearfix").select(".pl").text(),1, -4);//获取评价人数
				 int num = 0;
				 if(!number.equals("少于10")){
					 num = Integer.valueOf(number);
				 }
				 book.setNumber(number);
				 double score2 = 0.0;
				 if(num>=1000){
					 score2 = Double.valueOf(score);
					 score2 = Double.valueOf(score);
					 mapSort.put(href, book);
				 }
				 final String[] params = new String[]{bookName,author,date,value};
				 DataGet.log.debug("[NAME]{}",ArrayUtils.toString(params));
				 map.put(href,book);
			 }		
			 else if(str.length==4){
				 String author = str[0];//获取作者名
				 book.setAuthor(author);
				 String press = str[1];//获取出版社
				 String date = "";//获取出版日期
				 String value = str[3];//获取价钱
				 for(int i=0;i<str.length;i++){
					 if((str[i].trim().startsWith("20")) || (str[i].trim().startsWith("19"))){
						 date = str[i];
						 book.setDate(date);
					 if(i==2){
						 value = StringUtils.replace(str[i+1],"元","");
						 book.setValue(value);
						 press = str[i-1];
						 book.setPress(press);
					 }
					 }
				 }
				 String score = li.select(".info").select(".clearfix").select(".rating_nums").text();//获取评分
				 book.setScore(score);
				 String number = StringUtils.substring(li.select(".info").select(".clearfix").select(".pl").text(),1, -4);//获取评价人数
				 int num = 0;
				 if(!number.equals("少于10")){
					 num = Integer.valueOf(number);
				 }
				 book.setNumber(number);
				 double score2 = 0.0;
				 if(num>=1000){
					 score2 = Double.valueOf(score);
					 score2 = Double.valueOf(score);
					 mapSort.put(href, book);
				 }
				 final String[] params = new String[]{bookName,author,date,value};
				 DataGet.log.debug("[NAME]{}",ArrayUtils.toString(params));
				 map.put(href,book);
			 }
			 else if(str.length==2){
				 String author = str[0];//获取作者名
				 book.setAuthor(author);
				 String date = "";
				 String value = "";
				 if(str[1].trim().startsWith("20")||str[1].trim().startsWith("19")){
					 date = str[1];
					 book.setDate(date);
				 }else{
					 value = str[1];
					 book.setValue(value);
				 }
				 String score = li.select(".info").select(".clearfix").select(".rating_nums").text();//获取评分
				 book.setScore(score);
				 String number = StringUtils.substring(li.select(".info").select(".clearfix").select(".pl").text(),1, -4);//获取评价人数
				 int num = 0;
				 if(!number.equals("少于10")){
					 num = Integer.valueOf(number);
				 }
				 book.setNumber(number);
				 double score2 = 0.0;
				 if(num>=1000){
					 score2 = Double.valueOf(score);
					 mapSort.put(href, book);
				 }
				 final String[] params = new String[]{bookName,author,date};
				 DataGet.log.debug("[NAME]{}",ArrayUtils.toString(params));	
				 map.put(href,book);
			 }
		 }
	  }while(!wrong.equals("没有找到符合条件的图书"));	  		
}
	  /*
	   * 获取编程方面的书
	   */
	  
	@SuppressWarnings("unused")
	public static void dataGetProgram(){
	  String urlPre = "https://book.douban.com/tag/编程?start=";
	  
	  String urlSuf= "&type=T";
	  
	  Document doc = null;
	  String wrong = "";  
	  int count = 0;

	  do{
		  String numberStr = String.valueOf(count);
		  String url = urlPre+numberStr+urlSuf;//获取每一页编程数据信息
		  count = count + 20;
			 
		  try {
			doc = Jsoup.connect(url).maxBodySize(10 * 1024 * 1024).timeout(20000).get();
			wrong = doc.select(".pl2").get(0).text();
		  }catch (final Exception e) {
		      DataGet.log.error("[DATAGET-ERROR] {}", e);
		  	}
		  
		 final Elements uls = doc.select("ul");
		 
		 final Element ul = uls.get(2);
		 
		 final Elements lis = ul.select("li");
	 
		 for(final Element li : lis){
			 Book book = new Book();
				
			 String bookName = li.select(".info").select("a").get(0).text();//获取书名
			 String href = li.select(".info").select("a").attr("href");//用来判断某本书是否同时属于互联网,编程,算法中的两种或三种
			 book.setBookName(bookName);
			 String inf = li.select(".pub").text();
			 String str[] = inf.split("/");
			 if(str.length>4){
				 String author = str[0];//获取作者名
				 book.setAuthor(author);
				 String press = str[str.length-3];//获取出版社
				 book.setPress(press);
				 String date = str[str.length-2];//获取出版日期
				 book.setDate(date);
				 String value = StringUtils.replace(str[str.length-1],"元","");//获取价钱
				 book.setValue(value);
				 String score = li.select(".info").select(".clearfix").select(".rating_nums").text();//获取评分
				 book.setScore(score);
				 String number = StringUtils.substring(li.select(".info").select(".clearfix").select(".pl").text(),1, -4);//获取评价人数
				 int num = 0;
				 double score2 = 0.0;
				 if(!number.equals("少于10")){
					 num = Integer.valueOf(number);
				 }
				 book.setNumber(number);
				 if(num>=1000){
					 score2 = Double.valueOf(score);
					 mapSort.put(href, book);
				 }
				 final Object[] params = new Object[]{bookName,score,num,author,press,date,value};
				 DataGet.log.debug("[NAME]{}",ArrayUtils.toString(params));
				 map.put(href,book);
			 }
			 else if(str.length==3){
				 String author = str[0];//获取作者名
				 book.setAuthor(author);
				 String press = str[1];//获取出版社
				 String date = "";//获取出版日期
				 String value = "";//获取价钱
				 for(int i=0;i<str.length;i++){
					 if(str[i].trim().startsWith("20")||str[i].trim().startsWith("19")){
						 date = str[i]; 
						 book.setDate(date);
					 if(i==1){
						 value = StringUtils.replace(str[i+1],"元","");
						 book.setDate(date);
					 }if(i==1){
						 press = str[i-1];
						 book.setPress(press);
					 }
					 }
				 }
				 String score = li.select(".info").select(".clearfix").select(".rating_nums").text();//获取评分
				 book.setScore(score);
				 String number = StringUtils.substring(li.select(".info").select(".clearfix").select(".pl").text(),1, -4);//获取评价人数
				 int num = 0;
				 if(!number.equals("少于10")){
					 num = Integer.valueOf(number);
				 }
				 book.setNumber(number);
				 double score2 = 0.0;
				 if(num>=1000){
					 score2 = Double.valueOf(score);
					 score2 = Double.valueOf(score);
					 mapSort.put(href, book);
				 }
				 final String[] params = new String[]{bookName,author,date,value};
				 DataGet.log.debug("[NAME]{}",ArrayUtils.toString(params));
				 map.put(href,book);
			 }		
			 else if(str.length==4){
				 String author = str[0];//获取作者名
				 book.setAuthor(author);
				 String press = str[1];//获取出版社
				 String date = "";//获取出版日期
				 String value = str[3];//获取价钱
				 for(int i=0;i<str.length;i++){
					 if((str[i].trim().startsWith("20")) || (str[i].trim().startsWith("19"))){
						 date = str[i];
						 book.setDate(date);
					 if(i==2){
						 value = StringUtils.replace(str[i+1],"元","");
						 book.setValue(value);
						 press = str[i-1];
						 book.setPress(press);
					 }
					 }
				 }
				 String score = li.select(".info").select(".clearfix").select(".rating_nums").text();//获取评分
				 book.setScore(score);
				 String number = StringUtils.substring(li.select(".info").select(".clearfix").select(".pl").text(),1, -4);//获取评价人数
				 int num = 0;
				 if(!number.equals("少于10")){
					 num = Integer.valueOf(number);
				 }
				 book.setNumber(number);
				 double score2 = 0.0;
				 if(num>=1000){
					 score2 = Double.valueOf(score);
					 score2 = Double.valueOf(score);
					 mapSort.put(href, book);
				 }
				 final String[] params = new String[]{bookName,author,date,value};
				 DataGet.log.debug("[NAME]{}",ArrayUtils.toString(params));
				 map.put(href,book);
			 }
			 else if(str.length==2){
				 String author = str[0];//获取作者名
				 book.setAuthor(author);
				 String date = "";
				 String value = "";
				 if(str[1].trim().startsWith("20")||str[1].trim().startsWith("19")){
					 date = str[1];
					 book.setDate(date);
				 }else{
					 value = str[1];
					 book.setValue(value);
				 }
				 String score = li.select(".info").select(".clearfix").select(".rating_nums").text();//获取评分
				 book.setScore(score);
				 String number = StringUtils.substring(li.select(".info").select(".clearfix").select(".pl").text(),1, -4);//获取评价人数
				 int num = 0;
				 if(!number.equals("少于10")){
					 num = Integer.valueOf(number);
				 }
				 book.setNumber(number);
				 double score2 = 0.0;
				 if(num>=1000){
					 score2 = Double.valueOf(score);
					 mapSort.put(href, book);
				 }
				 final String[] params = new String[]{bookName,author,date};
				 DataGet.log.debug("[NAME]{}",ArrayUtils.toString(params));	
				 map.put(href,book);
			 }
		 }
	  }while(!wrong.equals("没有找到符合条件的图书"));	  		
}
 }

